package com.example.jf.jfapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;

import com.example.jf.jfapp.adapter.BlogCommentAdapter;
import com.example.jf.jfapp.bean.request.BlogCommentRequest;
import com.example.jf.jfapp.bean.response.BlogCommentResponse;
import com.example.jf.jfapp.bean.response.BlogResponse;
import com.example.jf.jfapp.utils.Constant;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.jf.jfapp.utils.OkHttpUtils.getOkHttpClient;
import static com.example.jf.jfapp.utils.RequestUtils.requestPOST;

public class BlogCommentActivity extends AppCompatActivity {
    public static final String REAMBEAN = "ReanBean";
//ces
    //ces
    private static final String TAG ="BlogCommentActivity";
    @BindView(R.id.back_blog)
    ImageView backBlog;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.blogtalkcomment)
    RecyclerView blogtalkcomment;
    private BlogResponse.ResultBean mResultBean;

    private BlogCommentRequest mBlogCommentRequest;

    ArrayList<BlogCommentResponse.ResultBean> mComResultBean=new ArrayList<>();

    private BlogCommentAdapter mBlogCommentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_comment);
        ButterKnife.bind(this);

        mResultBean = getIntent().getParcelableExtra(REAMBEAN);

        blogtalkcomment.setLayoutManager(new LinearLayoutManager(this));
        mBlogCommentAdapter=new BlogCommentAdapter(this,mComResultBean);
        blogtalkcomment.setAdapter(mBlogCommentAdapter);
        mBlogCommentRequest=new BlogCommentRequest(mResultBean.getBlogId(),10,0);


        getCommentData();

    }

    private void getCommentData() {
        final Request request = requestPOST(mBlogCommentRequest, Constant.URL.BLOG_COMMENT);
        OkHttpClient okHttpClient = getOkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "博客评论失败 ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Log.e(TAG, "博客评论: "+result);
                if (result != null) {
                    BlogCommentResponse blogCommentResponse=new Gson().fromJson(result,BlogCommentResponse.class);
                    mComResultBean.addAll(blogCommentResponse.getResult());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBlogCommentAdapter.notifyDataSetChanged();
                        }
                    });
                }

            }
        });
    }

    @OnClick(R.id.back_blog)
    public void onViewClicked() {
        finish();
    }
}
